<template>
  <custom-container ref="customContainerRef" :config="mainConfig" />
  <setForm
    ref="setFormRef"
    v-model:title="title"
    v-model:open="open"
    @changeList="handleQuery"
  />
</template>
<script lang="ts" setup>
import { Delete, Refresh, Plus, Download, Edit, Document } from '@element-plus/icons-vue'
import { ref, getCurrentInstance, defineAsyncComponent, computed } from 'vue'
import { ElMessage, ElMessageBox, ElButton } from 'element-plus'
import useDict, { DictDirection } from '@/store/modules/dicts'
import { listNotice, getNotice, delNotice, addNotice, updateNotice, delFile } from '@/api/system/announcements'

const setForm = defineAsyncComponent(() => import('./components/setForm.vue'))
let customContainerRef = ref(null),

  ids = [],
  // 非单个禁用
  single = ref(true),
  // 非多个禁用
  multiple = ref(true),
  title = ref(''),
  open = ref(false),
  // 查询参数
  queryParams = ref({
    pageNum: 1,
    pageSize: 10,
  }),
  setFormRef = ref(null),
  {
    proxy,
  }:{
  proxy:any
} = getCurrentInstance()

// 搜索
const handleQuery = () => {
  customContainerRef.value.getList()
}
// 列表选择
const handleSelectionChange = (rows: { [key: string]: any }[]) => {
  const rowsList = JSON.parse(JSON.stringify(rows))
  ids = rowsList.map((item) => item.noticeId)
  single.value = rowsList.length != 1
  multiple.value = !rowsList.length
}
// 新增通知
const handleAdd = () => {
  open.value = true
  title.value = '新增通知'
}
// 修改通知
const handleUpdate = (row: { [key: string]: any }) => {
  setFormRef.value.open = true
  const noticeId = row?.noticeId || ids
  setFormRef.value.title = '修改通知'
  setFormRef.value.getList(noticeId) 
}
// 删除通知
const handleDelete = (row: { [key: string]: any }) => {
  const id = row.noticeId || ids
  ElMessageBox.confirm(`是否确认删除用户编号为"${ id }"的数据项?`, '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
  .then(() => delNotice(id))  
  .then((res) => {
    if (res.code === 200) {
      handleQuery()
      ElMessage.success('删除成功')
    } else {
      ElMessage.error(res.msg)
    }
  })
  .catch((error) => {
    if (error == 'cancel') {
      ElMessage.error(error)
    }
  })
}
// 主体配置
const mainConfig = computed({
  get() {
    return {
      query: {
        fields: [
          {
            label: '公告标题',
            prop: 'noticeTitle',
            option: {
              placeholder: '请输入公告标题',
            },
          },
          {
            label: '类型',
            prop: 'noticeType',
            type: 'select',
            option: {
              placeholder: '请输入公告类型',
              dictInfo: {
                dict: DictDirection.通知公告,
              },
            },
          },
        ],
        queryParams: queryParams.value,
      },
      btns: [
        {
          title: '新增',
          icon: Plus,
          directive: [['hasPermi', ['system:notice:add']]],
          onClick: handleAdd,
        },
        {
          title: '修改',
          icon: Edit,
          type: 'success',
          disabled: single.value,
          directive: [['hasPermi', ['system:notice:edit']]],
          onClick: handleUpdate,
        },
        {
          title: '删除',
          icon: Delete,
          type: 'danger',
          disabled: multiple.value,
          directive: [['hasPermi', ['system:notice:remove']]],
          onClick: handleDelete,
        },
      ],
      table: {
        columns: [
          { type: 'selection', minWidth: '55', align: 'center' },
          {
            label: '序号',
            prop: 'noticeId',
            align: 'center',
            minWidth: '90',
          },
          {
            label: '公告标题',
            prop: 'noticeTitle',
            align: 'center',
            showOverflowTooltip: true,
          },
          {
            label: '公告类型',
            prop: 'noticeType',
            align: 'center',
            dictInfo: {
              dict: DictDirection.通知公告,
            },
          },
          {
            label: '状态',
            prop: 'status',
            align: 'center',
            autoFormatter: {
              dict: DictDirection.通用状态,
              rowKey: 'status',
            },
          },
          {
            label: '创建者',
            prop: 'createBy',
            align: 'center',
          },
          {
            label: '创建时间',
            prop: 'createTime',
            align: 'center',
          },
          {
            label: '操作',
            align: 'center',
            render: (h, params) => {
              const buttons = [
                {
                  title: '修改',
                  icon: Edit,
                  clickFun: handleUpdate,
                  directive: [['hasPermi', ['system:notice:edit']]],
                },
                {
                  title: '删除',
                  icon: Delete,
                  clickFun: handleDelete,
                  directive: [['hasPermi', ['system:notice:remove']]],
                },
              ]
              return buttons.map((el) => h(
                ElButton,
                {
                  text: true,
                  type: 'primary',
                  icon: el.icon,
                  onClick: () => {
                    el.clickFun(params.row)
                  },
                },
                {
                  default: () => el.title,
                },
              ))
            },
          },
        ],
        event: {
          onSelectionChange: (rows) => {
            handleSelectionChange(rows)
          },
        },
      },
      api: listNotice,
    }
  },
  set(val) {
    return val
  },
})
</script>
